package com.flycms.web.system.rest;

import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.flycms.core.base.BaseController;
import com.flycms.core.entity.PagingParam;
import com.flycms.core.entity.RestDataVo;
import com.flycms.core.utils.SnowFlake;
import com.flycms.core.utils.web.utils.CommonUtils;
import com.flycms.module.soft.model.ApplyTool;
import com.flycms.module.soft.model.SoftApply;
import com.flycms.module.soft.service.SoftApplyService;

/**
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年7月6日
 * @Date 2020年7月6日
 */

@RestController
@RequestMapping("/sa/system")
public class AdminSoftApplyController extends BaseController {
    
    private static final Logger logger = LogManager.getLogger(AdminSoftApplyController.class);
    
    @Autowired
    protected SoftApplyService softApplyService;
    
    @GetMapping(value = "/applys")
    public RestDataVo getSoftApplyList(@RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "company", required = false) String company,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "softName", required = false) String softName,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        RestDataVo rest = null;
        PagingParam paging = CommonUtils.validParam(pageIndex, pageSize);
        SoftApply apply = new SoftApply();
        apply.setUserName(userName);
        apply.setCompany(company);
        apply.setPhone(phone);
        apply.setEmail(email);
        apply.setSoftName(softName);
        try {
            List<SoftApply> applyList = softApplyService.getSoftApplyList(apply, paging.getOffset(), paging.getRows());
            int count = softApplyService.getSoftApplyCount(apply);
            rest = RestDataVo.PAGE(applyList, paging.getPageIndex(), paging.getPageSize(), count);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("获取申请列表失败");
        }
        return rest;
    }
    
    @PostMapping(value = "/{id}/remove")
    public RestDataVo removeSoftApply(@PathVariable("id") Long id) {
        RestDataVo rest = null;
        try {
            softApplyService.removeSoftApply(id);
            rest = RestDataVo.OK("删除成功");
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("删除失败");
        }
        return rest;
    }
    
    @GetMapping(value = "/tools")
    public RestDataVo getApplyToolList(@RequestParam(value = "toolName", required = false) String toolName,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        RestDataVo rest = null;
        PagingParam paging = CommonUtils.validParam(pageIndex, pageSize);
        try {
            List<ApplyTool> toolList = softApplyService.getApplyToolList(toolName, paging.getOffset(), paging.getRows());
            int count = softApplyService.getApplyToolCount(toolName);
            rest = RestDataVo.PAGE(toolList, paging.getPageIndex(), paging.getPageSize(), count);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("获取申请工具列表失败");
        }
        return rest;
    }
    
    @PostMapping(value = "/tool/add")
    public RestDataVo addApplyTool(@Valid @RequestBody ApplyTool apply, BindingResult errors) {
        String validResult = CommonUtils.validParams(errors, logger);
        if (StringUtils.isNotEmpty(validResult)) {
            return RestDataVo.ERROR(validResult);
        }
        RestDataVo rest = null;
        ApplyTool tool = new ApplyTool();
        Long id = new SnowFlake(2, 3).nextId();
        tool.setId(id);
        tool.setToolName(apply.getToolName());
        tool.setToolMsg(apply.getToolMsg());
        try {
            ApplyTool t0 = softApplyService.queryApplyToolByName(apply.getToolName());
            if (t0 != null) {
                rest = RestDataVo.ERROR("工具名已存在");
            } else {
                softApplyService.addApplyTool(tool);
                rest = RestDataVo.OK(tool);
            }
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("添加工具失败");
        }
        return rest;
    }
    
    @PostMapping(value = "/tool/{id}/update")
    public RestDataVo updateApplyTool(@RequestBody ApplyTool apply, @PathVariable("id") Long id) {
        RestDataVo rest = null;
        ApplyTool tool = new ApplyTool();
        tool.setId(id);
        tool.setToolName(apply.getToolName());
        tool.setToolMsg(apply.getToolMsg());
        try {
            softApplyService.updateApplyTool(tool);
            rest = RestDataVo.OK(tool);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("修改工具失败");
        }
        return rest;
    }
    
    @PostMapping(value = "/tool/{id}/remove")
    public RestDataVo removeApplyTool(@PathVariable("id") Long id) {
        RestDataVo rest = null;
        try {
            softApplyService.removeApplyTool(id);
            rest = RestDataVo.OK("删除成功");
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("删除工具失败");
        }
        return rest;
    }

}
